package com.caigou.modules.data.adpter

import android.app.Activity
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.R
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.bean.CommentItemBean
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.ext.GlideDisplay
import com.caigou.comm.base.ext.showImgsPreview
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.utils.ImageLoader
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lwkandroid.widget.ngv.DefaultNgvAdapter
import com.lwkandroid.widget.ngv.NgvChildImageView
import com.lwkandroid.widget.ngv.NineGridView

/**
 * @author #Suyghur.
 * Created on 2021/11/20
 */
class CommentDetailReplyItemAdapter(private val activity: Activity, private val rootItem: CommentItemBean, dataset: MutableList<CommentItemBean>) :
    BaseQuickAdapter<CommentItemBean, BaseViewHolder>(R.layout.item_comment_detail_reply_list, dataset) {

    override fun convert(holder: BaseViewHolder, item: CommentItemBean) {
        if (TextUtils.isEmpty(item.replyUserInfoVo.headImg)) {
            holder.setImageResource(R.id.item_comment_detail_reply_iv_avatar1, R.drawable.img_comm_avatar)
        } else {
            ImageLoader.loadCircle(context, item.replyUserInfoVo.headImg, holder.getView(R.id.item_comment_detail_reply_iv_avatar1))
        }
        holder.setText(R.id.item_comment_detail_reply_tv_nickname1, item.replyUserInfoVo.nickName)
        holder.setText(R.id.item_comment_detail_reply_tv_content, item.content)

        holder.getView<NineGridView>(R.id.item_comment_detail_reply_ngv_display).run {
            if (item.imgs.size > 0) {
                visibility = View.VISIBLE
                setSingleImageSize(TypedValue.COMPLEX_UNIT_DIP, 150, 200)
                val ngvAdapter = DefaultNgvAdapter(100, GlideDisplay())
                ngvAdapter.dataList = item.imgs
                ngvAdapter.setOnChildClickListener(object : DefaultNgvAdapter.OnChildClickedListener<String> {
                    override fun onPlusImageClicked(plusImageView: ImageView?, dValueToLimited: Int) {

                    }

                    override fun onContentImageClicked(position: Int, data: String?, childImageView: NgvChildImageView?) {
                        data?.apply {
                            showImgsPreview(activity, item.imgs, item.imgs.indexOf(this))
                        }
                    }

                    override fun onImageDeleted(position: Int, data: String?) {
                    }

                })
                adapter = ngvAdapter
            } else {
                visibility = View.GONE
            }
        }

        if (item.replyUserInfoVo.userId == item.receiverUserInfoVo.userId) {
            // 自己回复自己
            holder.getView<TextView>(R.id.item_comment_detail_reply_tv_desc).visibility = View.GONE
            holder.getView<ImageView>(R.id.item_comment_detail_reply_iv_avatar2).visibility = View.GONE
            holder.getView<TextView>(R.id.item_comment_detail_reply_tv_nickname2).visibility = View.GONE
        } else {
            // 别人回复自己
            if (TextUtils.isEmpty(item.receiverUserInfoVo.headImg)) {
                holder.setImageResource(R.id.item_comment_detail_reply_iv_avatar2, R.drawable.img_comm_avatar)
            } else {
                ImageLoader.loadCircle(context, item.receiverUserInfoVo.headImg, holder.getView(R.id.item_comment_detail_reply_iv_avatar2))
            }
            holder.setText(R.id.item_comment_detail_reply_tv_nickname2, item.receiverUserInfoVo.nickName)
        }
        holder.getView<Button>(R.id.item_comment_detail_reply_btn_reply).setOnClickListener {
            if (AppBackgroundInfo.instance.hasLogin) {
                ARouter.getInstance().build(RouterPath.Data.PAGE_COMMENT_REPLY)
                    .withObject(AppConstants.COMMENT_ROOT_ITEM_BEAN, rootItem)
                    .withObject(AppConstants.COMMENT_ITEM_BEAN, item)
                    .navigation()
            } else {
                context.showToast("请您先登录")
            }
        }
    }
}